package com.example.sunmiv1.adapter;

import android.content.Context;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.WarehouseInAndOutDetailBean;
import com.example.sunmiv1.utils.net.ACache;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OutAndInWarehouseDetailsAdapter extends RecyclerView.Adapter<OutAndInWarehouseDetailsAdapter.oaiwAdapter> {

    private List<WarehouseInAndOutDetailBean.DataBean> mList;
    private int mType;
    private WarehouseInAndOutDetailBean.DataBean dataBean;
    private Context mContext;
    private String pricePermission;// 門店查看價格權限

    public OutAndInWarehouseDetailsAdapter(Context context, List<WarehouseInAndOutDetailBean.DataBean> list, int type) {
        super();
        mContext = context;
        mList = list;
        mType = type;
        ACache aCache = ACache.get(context);
        pricePermission = aCache.getAsString("showPrice");
    }

    @Override
    public oaiwAdapter onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_outandin_warehouse_details, parent, false);
        return new oaiwAdapter(view);
    }

    @Override
    public void onBindViewHolder(oaiwAdapter viewHolder, int position) {
        dataBean = mList.get(position);

        viewHolder.goodName.setText(dataBean.getGoods_name());
        if (TextUtils.isEmpty(dataBean.getSku_name())) {
            viewHolder.layoutSkuName.setVisibility(View.GONE);
        } else {
            viewHolder.layoutSkuName.setVisibility(View.VISIBLE);
            viewHolder.skuName.setText(dataBean.getSku_name());
        }
        viewHolder.goodsCode.setText(dataBean.getGoods_code());
        viewHolder.goodsNumber.setText(dataBean.getGoods_number());
        viewHolder.skuNumber.setText(String.valueOf(dataBean.getSku_num()));

        if (mType == 1) {//1.AllocationAdapter入庫詳情 2.AllocationAdapter出庫詳情
            viewHolder.layoutInWarehouseNumber.setVisibility(View.VISIBLE);
            viewHolder.layoutInWarehousePrice.setVisibility(View.VISIBLE);
            viewHolder.layoutOutWarehouseNumber.setVisibility(View.GONE);
            viewHolder.layoutOutWarehousePrice.setVisibility(View.GONE);
            viewHolder.infoInWarehouseNumber.setText(String.valueOf(dataBean.getWarehouse_num()));
            viewHolder.infoInWarehousePrice.setText(String.valueOf(dataBean.getWarehouse_price()));
            ((View) viewHolder.infoOutBuildDate.getParent()).setVisibility(View.VISIBLE);
            ((View) viewHolder.infoOutQualityGuaranteePeriod.getParent()).setVisibility(View.VISIBLE);
            viewHolder.infoOutBuildDate.setText(dataBean.getBuild_date());
            viewHolder.infoOutQualityGuaranteePeriod.setText(dataBean.getQuality_guarantee_period());
            if ("1".equals(dataBean.getSerial_number_state())) {
                viewHolder.infoInWarehouseNumber.setTextColor(ContextCompat.getColor(mContext, R.color.blue_3293FA));
            } else {
                viewHolder.infoInWarehouseNumber.setTextColor(ContextCompat.getColor(mContext, R.color.text_666));
            }
            final int index = position;
            viewHolder.infoInWarehouseNumber.setOnClickListener(v -> {
                choose.click(index);
            });
        } else {// out
            viewHolder.layoutInWarehouseNumber.setVisibility(View.GONE);
            viewHolder.layoutInWarehousePrice.setVisibility(View.GONE);
            viewHolder.layoutOutWarehouseNumber.setVisibility(View.VISIBLE);
            viewHolder.layoutOutWarehousePrice.setVisibility(View.VISIBLE);
            viewHolder.infoOutWarehouseNumber.setText(String.valueOf(dataBean.getStock_num()));
            viewHolder.infoOutWarehousePrice.setText(String.valueOf(dataBean.getStock_price()));
            ((View) viewHolder.infoOutBuildDate.getParent()).setVisibility(View.GONE);
            ((View) viewHolder.infoOutQualityGuaranteePeriod.getParent()).setVisibility(View.GONE);
            if ("1".equals(dataBean.getSerial_number_state())) {
                viewHolder.infoOutWarehouseNumber.setTextColor(ContextCompat.getColor(mContext, R.color.blue_3293FA));
            } else {
                viewHolder.infoOutWarehouseNumber.setTextColor(ContextCompat.getColor(mContext, R.color.text_666));
            }
            final int index = position;
            viewHolder.infoOutWarehouseNumber.setOnClickListener(v -> {
                choose.click(index);
            });
        }

        viewHolder.infoMoneyTotal.setText(String.valueOf(dataBean.getSubtotal()));
        if (TextUtils.isEmpty(dataBean.getCompany())) {
            viewHolder.layoutUnit.setVisibility(View.GONE);
        } else {
            viewHolder.layoutUnit.setVisibility(View.VISIBLE);
            viewHolder.goodsUnit.setText(dataBean.getCompany());
        }
        if (TextUtils.isEmpty(dataBean.getRemarks())) {
            viewHolder.layoutRemark.setVisibility(View.GONE);
        } else {
            viewHolder.layoutRemark.setVisibility(View.VISIBLE);
            viewHolder.infoRemark.setText(dataBean.getRemarks());
        }

        if ("2".equals(pricePermission)) {
            viewHolder.layoutOutWarehousePrice.setVisibility(View.GONE);
            viewHolder.layoutInWarehousePrice.setVisibility(View.GONE);
            viewHolder.layoutMoneyTotal.setVisibility(View.GONE);
        }
    }

    private Choose choose;

    public void setChoose(Choose choose) {
        this.choose = choose;
    }

    public interface Choose {
        void click(int index);
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    static class oaiwAdapter extends RecyclerView.ViewHolder {
        @BindView(R.id.goods_name)
        TextView goodName;
        @BindView(R.id.layout_goods_name)
        RelativeLayout layoutGoodsName;
        @BindView(R.id.sku_name)
        TextView skuName;
        @BindView(R.id.layout_sku_name)
        RelativeLayout layoutSkuName;
        @BindView(R.id.goods_code)
        TextView goodsCode;
        @BindView(R.id.layout_goods_code)
        RelativeLayout layoutGoodsCode;
        @BindView(R.id.goods_number)
        TextView goodsNumber;
        @BindView(R.id.layout_goods_number)
        RelativeLayout layoutGoodsNumber;
        @BindView(R.id.sku_number)
        TextView skuNumber;

        @BindView(R.id.goods_unit)
        TextView goodsUnit;
        @BindView(R.id.layout_unit)
        RelativeLayout layoutUnit;
        @BindView(R.id.layout_sku_number)
        RelativeLayout layoutSkuNumber;
        @BindView(R.id.info_out_warehouse_number)
        TextView infoOutWarehouseNumber;
        @BindView(R.id.layout_out_warehouse_number)
        RelativeLayout layoutOutWarehouseNumber;
        @BindView(R.id.info_out_warehouse_price)
        TextView infoOutWarehousePrice;
        @BindView(R.id.layout_out_warehouse_price)
        RelativeLayout layoutOutWarehousePrice;
        @BindView(R.id.info_in_warehouse_number)
        TextView infoInWarehouseNumber;
        @BindView(R.id.layout_in_warehouse_number)
        RelativeLayout layoutInWarehouseNumber;
        @BindView(R.id.info_in_warehouse_price)
        TextView infoInWarehousePrice;
        @BindView(R.id.layout_in_warehouse_price)
        RelativeLayout layoutInWarehousePrice;
        @BindView(R.id.info_money_total)
        TextView infoMoneyTotal;
        @BindView(R.id.layout_money_total)
        RelativeLayout layoutMoneyTotal;
        @BindView(R.id.info_remark)
        TextView infoRemark;
        @BindView(R.id.layout_remark)
        RelativeLayout layoutRemark;
        @BindView(R.id.info_out_build_date)
        TextView infoOutBuildDate;
        @BindView(R.id.info_out_quality_guarantee_period)
        TextView infoOutQualityGuaranteePeriod;

        public oaiwAdapter(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
